package com.yiphonenum.phonenumberapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Iterator;

public class BackupFileListActivity extends AppCompatActivity {

    ListView listView;
    //BackupFileAdapter adapter;
    ArrayAdapter<FileData> adapter;
    ArrayList<String> files;
    ArrayList<FileData> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backup_file_list);

        arrayList = restoreContacts();
        if (arrayList != null) {
            listView = (ListView) findViewById(R.id.listview);
            adapter = new BackupFileAdapter(this, arrayList);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    TextView file = (TextView) view.getTag(R.id.file);
                    CheckBox checked = (CheckBox) view.getTag(R.id.checked);
                    String s = checked.isChecked() ? "checked" : "not checked";
                    Toast.makeText(getApplication(), file.getText().toString() + " " + s, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            finish();
            Toast.makeText(getApplication(), "没有备份文件", Toast.LENGTH_LONG).show();
        }

        files = new ArrayList<>();
    }

    public ArrayList<FileData> restoreContacts() {
        String storage_path = Environment.getExternalStorageDirectory().toString() + "/";
        ArrayList<FileData> arrayList_files = new ArrayList<>();
        File dir = new File(storage_path);
        File files[] = dir.listFiles(new MyFileter());

        for (File f : files) {
            FileData temp = new FileData(f.getName());
            arrayList_files.add(temp);
        }
        return arrayList_files.size() > 0 ? arrayList_files : null;

    }

    private class MyFileter implements FileFilter {
        @Override
        public boolean accept(File file) {
            if (file.isFile() && !file.isHidden()) {
                String file_name = file.getName();
                //return (file_name.startsWith("q_backup_") && file_name.endsWith(".vcf"));
                return (file_name.endsWith(".vcf"));
            }

            return false;
        }
    }


    // 按钮： 导入
    public void btnIpmortClick(View view) {
        ArrayList<String> restore_files = new ArrayList<>();
        // 遍历listview，取得文件

        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        String filetype = mime.getMimeTypeFromExtension("vcf");
        //
        String path = Environment.getExternalStorageDirectory().toString() + "/";

        for (FileData l : arrayList) {
            if (l.isSelected()) {
                files.add(l.getName());
            }
        }

        for (String f : files) {
            final File file = new File(path + f);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), filetype);
            startActivity(intent);
        }

    }

    // 按钮： 删除
    public void btnDelClick(View view) {
        String path = Environment.getExternalStorageDirectory().toString() + "/";
        Iterator<FileData> iterator = arrayList.iterator();
        if (iterator.hasNext()) {
            FileData f = iterator.next();
            if (f.isSelected()) {
                // 删除
                try {
                    File file = new File(path + f.getName());
                    if (file.delete())
                        if (file.delete()) {
                            arrayList.remove(f);
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } // 遍历
        adapter.clear();
        adapter.addAll(arrayList);
        adapter.setNotifyOnChange(true);
    }

    // end
}
